
#ifndef __xiong_sockethandler
#define __xiong_sockethandler
#include "Poco/AutoPtr.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketNotification.h"

#include "socketlist.h"


using Poco::AutoPtr;
using Poco::Net::StreamSocket;
using Poco::Net::ReadableNotification;
using Poco::Net::ShutdownNotification;


//for the accepted socket,process socket's reading and writing.
class EchoServiceHandler :public Poco::RefCountedObject
{
public:
    EchoServiceHandler(StreamSocket& socket):
        _socket(socket),
        _pBuffer(new char[BUFFER_SIZE])
    {
    }
    
    ~EchoServiceHandler()
    {
        std::cout << "delete self" << std::endl;
        _socket.close();
        delete [] _pBuffer;
    }
    
    void onReadable(const AutoPtr<ReadableNotification>& pNf)
    {
        int n = _socket.receiveBytes(_pBuffer, BUFFER_SIZE);
        if (n > 0)
            _socket.sendBytes(_pBuffer, n);
        else
            socketcloselist.addSocketEvent(_socket);
    }
    
    void onShutdown(const AutoPtr<ShutdownNotification>& pNf)
    {
            socketcloselist.addSocketEvent(_socket);
    }
    
private:
    enum
    {
        BUFFER_SIZE = 1024
    };
    
    StreamSocket   _socket;
    char*          _pBuffer;
};



#endif
